import pika
import requests
import pickle

# 1.建立RabbitMQ连接
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='127.0.0.1')
)	
# 2.声明频道对象
channel = connection.channel()
# 3.声明scrapy的队列
queueName = 'scrapy'
channel.queue_declare(queue=queueName, durable=True)
# 4.调用Channel的basic_publish方法，往scrapy队列内发送网络爬取到的Response信息
total = 10
for i in range(1, total+1):
    url = f'http://www.techlabplt.com:8080/BD-PC/priceList?pageId={i}'
    response = requests.get(url)
    channel.basic_publish(
        exchange='',
        routing_key=queueName,
        # 队列持久化存储
        properties=pika.BasicProperties(
            delivery_mode=2
        ),
        # 把对象序列化后以bytes对象返回。
        body=pickle.dumps(response)
    )
    print(f'发送从{response.url}得到的数据至消息队列')

